package com.ljs.shardingspheredemo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ljs.shardingspheredemo.entity.T_user;
import com.ljs.shardingspheredemo.mapper.UserMapper;
import com.ljs.shardingspheredemo.util.SnowFlake;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Controller
public class testController {
    @Resource
    UserMapper userMapper;
@RequestMapping("/add")
@ResponseBody
    public String test(){//循环插入值
    for(int i=0;i<60;i++){
        T_user user=new T_user();
     //user.setUserId(Long.valueOf(""+i));
//        SnowFlake snowFlake = new SnowFlake(2, 3); //mybatis要用这个，plus不用
//        Long id=snowFlake.createId();
//      user.setUserId(id);
        user.setName("shardingsphere");
        user.setAge(10);
        user.setGender(1);
        user.setIdNumber("2");
        userMapper.insert(user);
    }

         return "插入成功";
    }

    @RequestMapping("/selall")
    @ResponseBody
    public List<T_user> selall(){
        List<T_user> users=new ArrayList<>();
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.gt("user_id",563319959606792193l);
        Page page=new Page(2,4);//分页
        Page<T_user> page1=userMapper.selectPage(page,queryWrapper);
        users=userMapper.selectPage(page,queryWrapper).getRecords();
        users.forEach(User-> System.out.println(User));
        return users;
    }
    @RequestMapping("/selbuid")
    @ResponseBody
    public  T_user selbyid(){

       return  userMapper.selectById(563488971724689409l);
    }
    @RequestMapping("/between")
    @ResponseBody
    public  List<T_user> between(){
        List<T_user> users=new ArrayList<>();
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.between("user_id",563488971724689409l,563488983653289985l);
        users=   userMapper.selectList(queryWrapper);
        return  users;
    }
}
